package com.zd.mock.liruihe;

import com.github.javafaker.Faker;

import java.sql.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Locale;
import java.util.Random;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

public class HotelProvidersGenerator {

    private static final String DB_URL = "jdbc:mysql://hadoop102:3306/zhengzhou_tourism_business?useSSL=false&serverTimezone=UTC";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "000000";
    private static final int BATCH_SIZE = 100;
    private static final Faker faker = new Faker(new Locale("zh-CN"));
    private static final Random random = new Random();

    // 用于记录已生成的酒店名称，避免重复
    private static final Set<String> generatedNames = new HashSet<>();

    // 郑州区县列表
    private static final String[] ZHENGZHOU_DISTRICTS = {
            "金水区", "惠济区", "二七区", "中原区", "管城回族区",
            "郑东新区", "高新区", "经开区", "航空港区", "上街区",
            "登封市", "新密市", "荥阳市", "新郑市", "巩义市", "中牟县"
    };

    // 酒店品牌词库
    private static final String[] HOTEL_BRANDS = {
            "万豪", "洲际", "喜来登", "希尔顿", "皇冠", "温德姆", "香格里拉", "凯悦",
            "雅高", "丽思卡尔顿", "四季", "君悦", "威斯汀", "索菲特", "铂尔曼", "诺富特",
            "锦江", "如家", "汉庭", "全季", "格林豪泰", "维也纳", "亚朵", "桔子水晶"
    };

    // 酒店类型词库
    private static final String[] HOTEL_TYPES = {
            "国际酒店", "大酒店", "商务酒店", "精品酒店", "会议中心", "度假酒店",
            "连锁酒店", "主题酒店", "温泉酒店", "花园酒店", "公寓酒店", "行政酒店"
    };

    // 街道名称词库
    private static final String[] STREET_NAMES = {
            "金水路", "中原路", "建设路", "嵩山路", "大学路", "东风路",
            "航海路", "陇海路", "农业路", "花园路", "黄河路", "商都路",
            "郑开大道", "科学大道", "商务外环路", "CBD内环", "迎宾路", "少林大道"
    };

    // 地名前缀
    private static final String[] LOCATION_PREFIXES = {
            "中州", "大河", "天地", "世纪", "时代", "中原", "黄河", "嵩山",
            "少林", "商城", "商都", "绿城", "郑东", "经开", "航空港"
    };

    // 景点名称
    private static final String[] SCENIC_SPOTS = {
            "少林寺", "方特", "黄河游览区", "嵩山", "黄帝故里", "康百万庄园",
            "绿博园", "园博园", "只有河南", "银基", "伏羲山", "樱桃沟"
    };

    public static void main(String[] args) {
        int count = 1000;
        generateHotelProviders(count);
    }

    public static void generateHotelProviders(int count) {
        String insertSql = "INSERT INTO hotel_providers " +
                "(hotel_name, region_name, star_level, address, contact_phone, base_price, status, create_time, update_time) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(insertSql)) {

            conn.setAutoCommit(false);

            for (int i = 1; i <= count; i++) {
                // 生成酒店名称（确保唯一）
                String hotelName;
                int attempt = 0;
                do {
                    hotelName = generateHotelName();
                    attempt++;

                    // 防止无限循环
                    if (attempt > 50) {
                        hotelName += " (" + (i + 1000) + ")";
                        break;
                    }
                } while (generatedNames.contains(hotelName));

                generatedNames.add(hotelName);

                // 区域名称
                String regionName = generateRegionName();

                // 星级（控制比例）
                int starLevel = generateStarLevel();

                // 地址
                String address = generateAddress(regionName);

                // 联系电话
                String contactPhone = generateContactPhone(regionName);

                // 基础价格（基于星级）
                BigDecimal basePrice = generateBasePrice(starLevel);

                // 状态（98%正常，2%停用）
                int status = generateStatus();

                // 创建时间和更新时间
                Timestamp createTime = generateRandomTimestamp();
                Timestamp updateTime = generateUpdateTime(createTime);

                pstmt.setString(1, hotelName);
                pstmt.setString(2, regionName);
                pstmt.setInt(3, starLevel);
                pstmt.setString(4, address);
                pstmt.setString(5, contactPhone);
                pstmt.setBigDecimal(6, basePrice);
                pstmt.setInt(7, status);
                pstmt.setTimestamp(8, createTime);
                pstmt.setTimestamp(9, updateTime);

                pstmt.addBatch();

                if (i % BATCH_SIZE == 0) {
                    pstmt.executeBatch();
                }
            }

            pstmt.executeBatch();
            conn.commit();
            System.out.println("成功插入 " + count + " 条 hotel_providers 数据");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String generateHotelName() {
        int pattern = random.nextInt(5); // 增加到5种模式
        switch (pattern) {
            case 0: // 郑州+品牌+类型
                return "郑州" + HOTEL_BRANDS[random.nextInt(HOTEL_BRANDS.length)] +
                        HOTEL_TYPES[random.nextInt(HOTEL_TYPES.length)];

            case 1: // 区县+品牌+酒店
                return ZHENGZHOU_DISTRICTS[random.nextInt(ZHENGZHOU_DISTRICTS.length)] +
                        HOTEL_BRANDS[random.nextInt(HOTEL_BRANDS.length)] + "酒店";

            case 2: // 地名前缀+品牌+场所类型
                return LOCATION_PREFIXES[random.nextInt(LOCATION_PREFIXES.length)] +
                        HOTEL_BRANDS[random.nextInt(HOTEL_BRANDS.length)] +
                        (random.nextBoolean() ? "广场" : "大厦") + "酒店";

            case 3: // 景点+主题酒店
                return SCENIC_SPOTS[random.nextInt(SCENIC_SPOTS.length)] +
                        (random.nextBoolean() ? "主题" : "度假") + "酒店";

            default: // 品牌+特色+酒店
                String[] features = {"豪华", "精品", "商务", "会议", "温泉", "花园"};
                return HOTEL_BRANDS[random.nextInt(HOTEL_BRANDS.length)] +
                        features[random.nextInt(features.length)] + "酒店";
        }
    }

    private static String generateRegionName() {
        // 85%郑州区县，15%河南其他城市
        if (random.nextDouble() < 0.85) {
            return ZHENGZHOU_DISTRICTS[random.nextInt(ZHENGZHOU_DISTRICTS.length)];
        } else {
            String[] otherCities = {"洛阳", "开封", "安阳", "新乡", "焦作", "南阳"};
            String[] regionTypes = {"市辖区", "新区", "开发区", "城区"};

            return otherCities[random.nextInt(otherCities.length)] +
                    regionTypes[random.nextInt(regionTypes.length)];
        }
    }

    private static int generateStarLevel() {
        double rand = random.nextDouble();
        if (rand < 0.10) return 5;      // 10% 5星
        else if (rand < 0.35) return 4;  // 25% 4星
        else if (rand < 0.75) return 3;  // 40% 3星
        else return random.nextInt(2) + 1; // 25% 1-2星
    }

    private static String generateAddress(String regionName) {
        return regionName + STREET_NAMES[random.nextInt(STREET_NAMES.length)] +
                (random.nextInt(999) + 1) + "号";
    }

    private static String generateContactPhone(String regionName) {
        // 郑州区号0371，其他城市使用037X
        if (isZhengzhouDistrict(regionName)) {
            return "0371-" + (6000 + random.nextInt(3000)) + "" + (1000 + random.nextInt(9000));
        } else {
            int cityCode = 371 + random.nextInt(29); // 0371-0399
            return "0" + cityCode + "-" + (6000 + random.nextInt(3000)) + "" + (1000 + random.nextInt(9000));
        }
    }

    private static boolean isZhengzhouDistrict(String regionName) {
        for (String district : ZHENGZHOU_DISTRICTS) {
            if (district.equals(regionName)) {
                return true;
            }
        }
        return false;
    }

    private static BigDecimal generateBasePrice(int starLevel) {
        switch (starLevel) {
            case 5: return BigDecimal.valueOf(500 + random.nextDouble() * 500).setScale(2, BigDecimal.ROUND_HALF_UP);
            case 4: return BigDecimal.valueOf(300 + random.nextDouble() * 300).setScale(2, BigDecimal.ROUND_HALF_UP);
            case 3: return BigDecimal.valueOf(200 + random.nextDouble() * 200).setScale(2, BigDecimal.ROUND_HALF_UP);
            default: return BigDecimal.valueOf(80 + random.nextDouble() * 220).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
    }

    private static int generateStatus() {
        // 98%正常营业，2%停用
        return random.nextDouble() < 0.98 ? 1 : 0;
    }

    private static Timestamp generateRandomTimestamp() {
        long start = LocalDate.of(2023, 1, 1).atStartOfDay(ZoneId.systemDefault()).toEpochSecond() * 1000;
        long end = System.currentTimeMillis();
        long randomTime = start + (long) (random.nextDouble() * (end - start));
        return new Timestamp(randomTime);
    }

    private static Timestamp generateUpdateTime(Timestamp createTime) {
        // 在创建时间基础上增加0-365天
        long offset = (long) (random.nextDouble() * 365L * 24 * 60 * 60 * 1000);
        return new Timestamp(createTime.getTime() + offset);
    }
}